package com.canbot.u05.activity.presidemode.newcode;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.canbot.u05.R;
import com.canbot.u05.activity.presidemode.newcode.adapter.NHostTaskListAdapter;
import com.canbot.u05.activity.presidemode.newcode.bean.HostModuleData;
import com.canbot.u05.activity.presidemode.newcode.bean.ModuleUtil;
import com.canbot.u05.activity.presidemode.newcode.bean.NModuleType;
import com.canbot.u05.activity.presidemode.newcode.bean.NetLoadUtil;
import com.canbot.u05.activity.presidemode.newcode.interfaces.IDialogBtnLisenter;
import com.canbot.u05.activity.presidemode.newcode.interfaces.IItemClicks;
import com.canbot.u05.activity.presidemode.newcode.interfaces.IPopItemClick;
import com.canbot.u05.activity.presidemode.newcode.interfaces.IResponse;
import com.canbot.u05.activity.presidemode.newcode.view.ItemPopWindow;
import com.canbot.u05.activity.presidemode.newcode.view.NHostBaseDialog;
import com.canbot.u05.activity.presidemode.newcode.view.NHostDeleteDialog;
import com.canbot.u05.activity.presidemode.newcode.view.NHostEditnameDialog;
import com.canbot.u05.bean.guide.MapVoiceAndEyes;
import com.canbot.u05.socket.MsgSendUtils;
import com.canbot.u05.socket.MsgType;
import com.canbot.u05.utils.ToastTools;
import com.canbot.u05.widget.LoadingDialogUtils;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 主持模式首页-- 展示任务列表
 */
public class NHostTaskListActivity extends BaseHostActivity {
    private Context mContext;
    @BindView(R.id.recycleView)
    RecyclerView recycleView;
    public static CopyOnWriteArrayList<HostModuleData> listData;
    private NHostTaskListAdapter nHostTaskListAdapter;
    private int editPos;

    @OnClick(R.id.back)
    public void exit() {
        finish();
    }

    @OnClick(R.id.add)
    public void addTask() {
        gotoActivityForResult(NhostTaskAddActivity.class, NModuleType.ADD_ITEM);
    }

    @Override
    protected void initEventData() {
        MapVoiceAndEyes.playMapVoiceAndEyes(MapVoiceAndEyes.voiceEyesList.get(18));
        mContext = this;
        recycleView.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false));
        listData = mockData();
        nHostTaskListAdapter = new NHostTaskListAdapter(listData, this, new IItemClicks() {
            @Override
            public void onMainItemClick(int pos, View view) {
                exeTask(pos);
            }

            @Override
            public void onClick(int position, View view) {
                showPopWindow(position, view);
            }
        });
        recycleView.setAdapter(nHostTaskListAdapter);
        getDataFromServer();
    }

    private void exeTask(int pos) {
        HostModuleData hostModuleData = listData.get(pos);
        dialogLoading = LoadingDialogUtils.createLoadingDialog(this, getString(R.string.loading));
        NetLoadUtil.handTaskStart(this, new IResponse<String>() {
            @Override
            public void onSucusse(String result) {
                LoadingDialogUtils.closeDialog(dialogLoading);
                Intent intent = new Intent(getApplication(), NHostExeActivity.class);
                intent.putExtra(DATA, hostModuleData);
                gotoActivity(intent);
            }

            @Override
            public void onFailuer(String result) {
                LoadingDialogUtils.closeDialog(dialogLoading);
                ToastTools.newInstance(getApplicationContext()).showText(result);
            }
        }, hostModuleData);
    }

    private Dialog dialogLoading;

    private void getDataFromServer() {
        dialogLoading = LoadingDialogUtils.createLoadingDialog(this, getString(R.string.loading));
        NetLoadUtil.loadAllTask(getApplicationContext(), new IResponse<List<HostModuleData>>() {
            @Override
            public void onSucusse(List<HostModuleData> result) {
                if (result != null && result.size() > 0) {
                    for (HostModuleData hostModuleData : result) {
                        Log.e(TAG, "onSucusse: " + hostModuleData);
                    }
                    listData.clear();
                    listData.addAll(result);
                    runOnUiThread(() -> nHostTaskListAdapter.notifyDataSetChanged());
                } else {
                    ToastTools.newInstance(getApplicationContext()).showText(getString(R.string.module_not_exist));
                }
                LoadingDialogUtils.closeDialog(dialogLoading);
            }

            @Override
            public void onFailuer(String result) {
                ToastTools.newInstance(getApplicationContext()).showText(result);
                LoadingDialogUtils.closeDialog(dialogLoading);
            }
        });
    }

    private void showPopWindow(final int position, View view) {
        ItemPopWindow itemPopWindow = new ItemPopWindow(this);
        itemPopWindow.show(view, new IPopItemClick() {
            @Override
            public void onEditor() {
                editPos = position;
                HostModuleData hostModuleData = listData.get(position);
                gotoActivityForResult(NhostTaskAddActivity.class, NModuleType.EDIT_ITEM, hostModuleData);
            }

            @Override
            public void onRename() {
                NHostBaseDialog nHostModeDialog = new NHostEditnameDialog(mContext, NHostEditnameDialog.RENAME);
                nHostModeDialog.setiDialogBtnLisenter((IDialogBtnLisenter<String>) result -> {
                    if (!ModuleUtil.checkNameExist(result, listData)) {
                        rename(result, position);
                    } else {
                        ToastTools.newInstance(getApplicationContext()).showText(getString(R.string.task_name_exist));
                    }
                });
                nHostModeDialog.show();
            }

            @Override
            public void onDelete() {
                NHostBaseDialog nHostModeDialog = new NHostDeleteDialog(mContext);
                nHostModeDialog.setiDialogBtnLisenter((IDialogBtnLisenter<String>) result -> {
                    deleteData(position);
                });
                nHostModeDialog.show();
            }
        });
    }

    private void deleteData(int position) {
        dialogLoading = LoadingDialogUtils.createLoadingDialog(this, getString(R.string.loading));
        HostModuleData hostModuleData = listData.get(position);
        NetLoadUtil.delelteOneTask(getApplicationContext(), new IResponse<String>() {
            @Override
            public void onSucusse(String result) {
                LoadingDialogUtils.closeDialog(dialogLoading);
                listData.remove(position);
                runOnUiThread(() -> nHostTaskListAdapter.notifyDataSetChanged());
            }

            @Override
            public void onFailuer(String result) {
                ToastTools.newInstance(getApplicationContext()).showText(result);
                LoadingDialogUtils.closeDialog(dialogLoading);
            }
        }, hostModuleData);
    }

    private void rename(String newName, int position) {
        HostModuleData hostModuleData = listData.get(position);
        dialogLoading = LoadingDialogUtils.createLoadingDialog(this, getString(R.string.loading));
        NetLoadUtil.renameOneTask(this, new IResponse<String>() {
            @Override
            public void onSucusse(String result) {
                LoadingDialogUtils.closeDialog(dialogLoading);
                hostModuleData.setTaskName(newName);
                runOnUiThread(() -> nHostTaskListAdapter.notifyItemChanged(position));
            }

            @Override
            public void onFailuer(String result) {
                ToastTools.newInstance(getApplicationContext()).showText(result);
                LoadingDialogUtils.closeDialog(dialogLoading);
            }
        }, newName, hostModuleData.getTaskName());

    }

    private CopyOnWriteArrayList<HostModuleData> mockData() {
        CopyOnWriteArrayList<HostModuleData> hostModuleDataList = new CopyOnWriteArrayList<>();
        return hostModuleDataList;
    }

    private static final String TAG = "NHostTaskListActivity";

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (data != null) {
            switch (requestCode) {
                case NModuleType.ADD_ITEM:
                    HostModuleData hostModuleData = (HostModuleData) data.getSerializableExtra(DATA);
                    if (hostModuleData != null) {
                        listData.add(hostModuleData);
                        nHostTaskListAdapter.notifyItemInserted(listData.size() - 1);
                    }
                    break;
                case NModuleType.EDIT_ITEM:
                    hostModuleData = (HostModuleData) data.getSerializableExtra(DATA);
                    Log.e(TAG, "onActivityResult: " + hostModuleData);
                    if (hostModuleData != null) {
                        listData.set(editPos, hostModuleData);
                    }
                    break;
            }
        }
    }

    @Override
    public void onBackPressed() {
        MsgSendUtils.sendStringMsg(MsgType.RECEIVER_3288_PRESIDE_EXIT, "主持人模式");
        super.onBackPressed();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (dialogLoading != null) {
            LoadingDialogUtils.closeDialog(dialogLoading);
        }
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_nhost_task_list;
    }
}
